/*
 * Copyright (C) 2024 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.wakala.employment.fragment.other;

import static com.xuexiang.xutil.display.DensityUtils.dip2px;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.tu.loadingdialog.LoadingDailog;
import com.kongzue.dialogx.dialogs.MessageDialog;
import com.kongzue.dialogx.interfaces.BottomDialogSlideEventLifecycleCallback;
import com.kongzue.dialogx.interfaces.OnBindView;
import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialogx.style.KongzueStyle;
import com.kongzue.dialogx.style.MIUIStyle;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnCancelListener;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.util.XPopupUtils;
import com.wakala.employment.adapter.entity.LoginInfo;
import com.wakala.employment.core.BaseFragment;
import com.wakala.employment.R;
import com.wakala.employment.core.http.subscriber.NoTipRequestSubscriber;
import com.wakala.employment.core.http.subscriber.TipProgressLoadingSubscriber;
import com.wakala.employment.core.http.subscriber.TipRequestSubscriber;
import com.wakala.employment.databinding.FragmentSettingsBinding;
import com.wakala.employment.http.TestApi;
import com.wakala.employment.utils.TokenUtils;
import com.wakala.employment.utils.sdkinit.XUpdateInit;
import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xhttp2.XHttpProxy;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xhttp2.subsciber.ProgressDialogLoader;
import com.xuexiang.xhttp2.subsciber.impl.IProgressLoader;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xui.widget.dialog.DialogLoader;
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView;
import com.xuexiang.xutil.XUtil;
import com.xuexiang.xutil.app.AppUtils;
import com.xuexiang.xutil.data.DateUtils;
import com.xuexiang.xutil.tip.ToastUtils;

import java.util.Date;

/**
 * @author xuexiang
 * @since 2019-10-15 22:38
 */
@Page(name = "设置")
public class SettingsFragment extends BaseFragment<FragmentSettingsBinding> implements SuperTextView.OnSuperTextViewClickListener {

    private IProgressLoader mIProgressLoader;

    @NonNull
    @Override
    protected FragmentSettingsBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentSettingsBinding.inflate(inflater, container, attachToRoot);
    }

    @Override
    protected void initViews() {
        mIProgressLoader = new ProgressDialogLoader(getContext(), "正在退出登录...");
        binding.menuCommon.setOnSuperTextViewClickListener(this);
        binding.menuPrivacy.setOnSuperTextViewClickListener(this);
        binding.menuPush.setOnSuperTextViewClickListener(this);
        binding.menuHelper.setOnSuperTextViewClickListener(this);
        binding.menuCheckUpdate.setOnSuperTextViewClickListener(this);
        binding.menuManageAccount.setOnSuperTextViewClickListener(this);
        binding.menuLogout.setOnSuperTextViewClickListener(this);
        binding.menuAbout.setOnSuperTextViewClickListener(this);
        //
        binding.tvCopyright.setText("Copyright 20022-"+ DateUtils.getYear(new Date())+" v"+ AppUtils.getAppVersionName());
    }

    @SingleClick
    @Override
    public void onClick(SuperTextView superTextView) {
        int id = superTextView.getId();
        if ( id == R.id.menu_helper) {
            XToastUtils.toast(superTextView.getLeftString());
        } else if (id == R.id.menu_common) {
            openNewPage(CommonConfigFragment.class);
        } else if (id == R.id.menu_push) {
            openNewPage(PushConfigFragment.class);
        } else if (id == R.id.menu_privacy) {
            openNewPage(PersonalRecommendFragment.class);
        } else if (id == R.id.menu_privacy) {
            openNewPage(PersonalRecommendFragment.class);
        } else if (id ==R.id.menu_about){
            //关于
            openNewPage(AboutFragment.class);
        }else if (id == R.id.menu_manage_account) {
            openNewPage(AccountManageFragment.class);
        }  else if (id == R.id.menu_logout) {
            MessageDialog.build(KongzueStyle.style()).show("系统提示", "确认退出当前账号？", "确定", "取消").setDialogLifecycleCallback(new BottomDialogSlideEventLifecycleCallback<MessageDialog>() {

                @Override
                public void onShow(MessageDialog dialog) {
                    super.onShow(dialog);
                    dialog.getDialogImpl().txtDialogTip.setPadding(0, dip2px(20), 0, 0);
                }
            }).setOkButton((messageDialog, view) -> {
                logout();
                return false;
            }).setCustomView(new OnBindView<MessageDialog>(R.layout.layout_custom_dialog_view) {
                @Override
                public void onBind(MessageDialog dialog, View v) {
                    v.findViewById(R.id.img_ico).setVisibility(View.GONE);
                    v.findViewById(R.id.tv_text).setVisibility(View.GONE);
                    v.findViewById(R.id.btn_close).setVisibility(View.GONE);
                }
            });
           /* new XPopup.Builder(getContext()).asConfirm("系统提示",  getString(R.string.lab_logout_confirm),  getString(R.string.lab_no), getString(R.string.lab_yes), new OnConfirmListener() {
                @Override
                public void onConfirm() {
                    logout();
                }
            }, new OnCancelListener() {
                @Override
                public void onCancel() {

                }
            },false).show();*/
           /* DialogLoader.getInstance().showConfirmDialog(
                    getContext(),
                    getString(R.string.lab_logout_confirm),
                    getString(R.string.lab_yes),
                    (dialog, which) -> {
                        dialog.dismiss();
                        //XUtil.getActivityLifecycleHelper().exit();
                        //TokenUtils.handleLogoutSuccess();
                        logout();
                    },
                    getString(R.string.lab_no),
                    (dialog, which) -> dialog.dismiss()
            );*/
        }
    }

    /**
     * 调用接口退出
     */
    @SuppressLint("CheckResult")
    private void logout() {
        LoadingDailog.Builder loadBuilder=new LoadingDailog.Builder(getContext())
                .setMessage("正在退出登录...")
                .setCancelable(true)
                .setCancelOutside(true);
        LoadingDailog dialog=loadBuilder.create();
        dialog.show();
        XHttpProxy.proxy(TestApi.IAuthorization.class)
                .loginOut()
                .subscribeWith(new NoTipRequestSubscriber<String>() {
                    @Override
                    public void onSuccess(String string) {
                        dialog.dismiss();
                        XUtil.getActivityLifecycleHelper().exit();
                        TokenUtils.handleLogoutSuccess();
                    }
                    @Override
                    public void onError(ApiException e) {
                        dialog.dismiss();
                        super.onError(e);
                        ToastUtils.toast(e.getDisplayMessage());
                    }
                });
    }

}
